//
//  MiddleView.m
//  飞天FM
//
//  Created by Song on 2017/6/11.
//  Copyright © 2017年 SongZhaoJun. All rights reserved.
//

#import "MiddleView.h"
#import "UIView+Extension.h"
#import "UIColor+Extension.h"
#import "UIImage+FTImage.h"
#import "CALayer+PauseAimate.h"
#import "Common.h"

@interface MiddleView()


/**
 显示图片View
 */
@property (weak, nonatomic) IBOutlet UIImageView *middleImageView;

/**
 播放按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *playBtn;

@end

@implementation MiddleView

static MiddleView* _shareInstance;

+(instancetype)shareInstance {
    
    if (_shareInstance == nil) {
        _shareInstance = [MiddleView middleView];
    }
    return _shareInstance;
}


+ (instancetype)middleView {
    
    NSBundle* currentBundl = [NSBundle bundleForClass:self];
    MiddleView* middleView = [[currentBundl loadNibNamed:@"MiddleView" owner:nil options:nil] firstObject];
  
    return middleView;
}


-(void)awakeFromNib {
    
    [super awakeFromNib];
    
    self.middleImageView.layer.masksToBounds = true;
    self.middleImg = self.middleImageView.image;
    
    NSBundle* currentBundl = [NSBundle bundleForClass:self.class];
    NSString* imagePath = [currentBundl pathForResource:@"zxy_icon@2x.jpg" ofType:nil inDirectory:@"FTFMMain.bundle"];
    self.middleImg = [UIImage imageWithContentsOfFile:imagePath];
    
    
    // 创建动画
    [self.middleImageView.layer removeAnimationForKey:@"playAnnimation"];
    CABasicAnimation* basicAnimation = [[CABasicAnimation alloc] init];
    basicAnimation.keyPath = @"transform.rotation.z";
    basicAnimation.fromValue = @0;
    basicAnimation.toValue = @(M_PI * 2);
    basicAnimation.duration = 30;
    basicAnimation.repeatCount = MAXFLOAT;
    basicAnimation.removedOnCompletion = false;
    [self.middleImageView.layer addAnimation:basicAnimation forKey:@"playAnnimation"];
    
    [self.middleImageView.layer pauseAnimate];
    
    [self.playBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 监听播放状态的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isPlayerPlay:) name:MiddleViewPlayState object:nil];
    
   
    // 监听播放图片的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setPlayImage:) name:MiddleViewPlayImage object:nil];
    
}

#pragma mark- 事件监听
- (void)isPlayerPlay:(NSNotification*)notification {
    
    BOOL isPlay = [notification.object boolValue];
    self.isPlaying = isPlay;
}

- (void)setPlayImage:(NSNotification*)notification {
    UIImage* image = notification.object;
    self.middleImg = image;
}

-(void) btnClick:(UIButton*)btn {
    
    if (self.middleClickBlock) {
        self.middleClickBlock(self.isPlaying);
    }
}

-(void)setIsPlaying:(BOOL)isPlaying {
    
    if (_isPlaying == isPlaying) {
        return;
    }
    
    _isPlaying = isPlaying;
    
    if (isPlaying) {
        [self.playBtn setImage:nil forState:UIControlStateNormal];
        [self.middleImageView.layer resumeAnimate];
    } else {
        
        // 当前bundle
        NSBundle* currentBundle = [NSBundle bundleForClass:self.class];
        // 图片路径
        NSString* imagePath = [currentBundle pathForResource:@"tabbar_np_play@2x.png" ofType:nil inDirectory:@"FTFMMain.bundle"];
        UIImage* image = [UIImage imageWithContentsOfFile:imagePath];
        [self.playBtn setImage:image forState:UIControlStateNormal];
        [self.middleImageView.layer pauseAnimate];
    }
}

-(void)setMiddleImg:(UIImage *)middleImg {
    
    _middleImg = middleImg;
    
    self.middleImageView.image = middleImg;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    self.middleImageView.layer.cornerRadius = self.middleImageView.width * 0.5;
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

